﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;
using System.Data;
using System.Collections;
using QLNhaThuoc.MyClass;

namespace QLNhaThuoc.DAL
{
    class PhieuNhapDA : DataProvider, IMaObserver
    {
        private CTPhieuNhapDA ctpnDA = new CTPhieuNhapDA();

        public PhieuNhapDA()
        {
            connect();
            fillMyTable("PHIEU_NHAP");
        }

        public void insert(PhieuNhap info)
        {
            DataRow row = myTable.NewRow();
            //row["MaPN"] = info.MaPN;
            row["MaNV"] = info.MaNV;
            row["NgayNhap"] = info.NgayNhap;
            row["ThanhTien"] = info.ThanhTien;
            row["MaCongTy"] = info.MaCongTy;
            row["TienNo"] = info.TienNo;
            myTable.Rows.Add(row);

            updateMyTable();

            foreach (CTPhieu ctpn in info.ctPhieuNhap)
            {
                ctpn.Ma = int.Parse(row["MaPN"].ToString());
                ctpnDA.insert(ctpn);
            }
        }

        public void delete(PhieuNhap info)
        {
            foreach (CTPhieu ctpn in info.ctPhieuNhap)
            {
                ctpnDA.delete(ctpn);
            }

            myTable.Rows.Find(info.MaPN).Delete();

            updateMyTable();           
        }

        public void updatePhieuNhap(string maCongTyCu, string maCongTyMoi)
        {
            foreach (DataRow row in myTable.Rows)
            {
                if (row["MaCongTy"].ToString().Trim().CompareTo(maCongTyCu) == 0)
                {
                    row["MaCongTy"] = maCongTyMoi;
                }
            }

            updateMyTable();
        }

        public void update(PhieuNhap info)
        {
            DataRow row = myTable.Rows.Find(info.MaPN);
            //row["MaPN"] = info.MaPN;
            row["MaNV"] = info.MaNV;
            row["NgayNhap"] = info.NgayNhap;
            row["ThanhTien"] = info.ThanhTien;
            row["MaCongTy"] = info.MaCongTy;
            row["TienNo"] = info.TienNo;

            updateMyTable();

            foreach (CTPhieu ctpn in info.ctPhieuNhap)
            {
                ctpnDA.update(ctpn);
            }
        }

        public bool KiemTraPK(PhieuNhap info)
        {
            DataRow row = myTable.Rows.Find(info.MaPN);
            if (row != null)
                return true;
            return false;
        }

        public PhieuNhap[] search(string expr)
        {
            string sqlString = "select * from PHIEU_NHAP where " + expr;
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);
            int n = list.Count;
            PhieuNhap[] kq = new PhieuNhap[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (PhieuNhap)list[i];
            }

            return kq;
        }

        public PhieuNhap[] LayListPhieuNhap()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            PhieuNhap[] kq = new PhieuNhap[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (PhieuNhap)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable table, int i)
        {
            PhieuNhap obj = new PhieuNhap();
            obj.MaPN = int.Parse(table.Rows[i]["MaPN"].ToString());
            obj.MaNV = table.Rows[i]["MaNV"].ToString();
            obj.NgayNhap = DateTime.Parse(table.Rows[i]["NgayNhap"].ToString());
            obj.ThanhTien = int.Parse(table.Rows[i]["ThanhTien"].ToString());
            obj.MaCongTy = table.Rows[i]["MaCongTy"].ToString();
            obj.TienNo = int.Parse(table.Rows[i]["TienNo"].ToString());
            obj.ctPhieuNhap = ctpnDA.LayListCTPhieu(obj.MaPN);

            return obj;
        }

        public void UpdateMaCongTy(object old_info, object info)
        {
            updatePhieuNhap(((CongTy)old_info).MaCT, ((CongTy)info).MaCT);
        }
    }
}
